<template>
  <el-dialog
    :visible.sync="visible"
    title="#出库计划录入"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="砂芯图号" prop="spuNo">
            <el-select
              v-model="dataForm.spuNo"
              filterable
              placeholder="请选择砂芯"
            >
              <el-option
                v-for="item in spuOptions"
                :key="item.spuId"
                :label="item.spuNo"
                :value="item.spuNo"
              >
                <span style="float: left">{{ item.spuNo }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{
                  item.spuName
                }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="需求数量" prop="amount">
            <el-input-number
              v-model="dataForm.amount"
              :min="1"
              :max="100"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="需求站台" prop="stationNo">
        <el-radio-group v-model="dataForm.stationNo" size="small">
          <el-radio border v-for="item in stnOptions" :key="item.stationId" :label="item.stationNo">{{item.stationDescription}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="comments">
        <el-input type="textarea" v-model="dataForm.comments"></el-input>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        orderId: '',
        orderType:'出库下架',
        spuNo: '',
        amount: '',
        stationNo: '',
        comments: '',
      },
      stationType: "CTN",
      spuOptions: [],
      stnOptions: [],
    };
  },
  computed: {
    dataRule() {
      return {
        spuNo: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        amount: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        stationNo: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    init() {
      this.visible = true;
      //重置一下计划信息
      this.dataForm = this.$options.data().dataForm;
      this.getSpuInfo();
      this.getStnInfo();
    },
    // 1.获取砂芯图号
    getSpuInfo() {
      this.$http
        .get(`/wms/spu-info/list`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.spuOptions = res.data;
        })
        .catch(() => {});
    },
    //2.获取站台信息
    getStnInfo() {
      this.$http
        .get(`/wms/station/list`, {
          params: {
            stationType: this.stationType,
          },
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.stnOptions = res.data;
          console.log("stnOption:", this.stnOptions);
        })
        .catch(() => {});
    },
    // 3.表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.orderId ? "post" : "put"](
            "/wms/order/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
